<?php

namespace App\Services;

use App\Models\Letter;
use Carbon\Carbon;

class LetterService
{

    public function writeLetterService($request){

        $data = $request->all();

        $validator = Letter::checkValidate($request->all());

        if ($validator->fails()) {
            return activeFail('',$validator->errors()->first());
        }
        $send_time = $request->input('send_time');

        $check_time = Carbon::now()->addDays(1)->toDateTimeString();

        if ($send_time < $check_time) {
            return activeFail('','系统限制最早发信时间须在 '.$check_time. ' 之后');
        }

        $emailInfo = [
            'title' => $data['title'] ?? '写给未来的一封信',
            'username' => $data['username'],
            'email' => $data['email'],
            'send_time' => $send_time,
            'is_public' => $data['is_public'] ?? 0,
            'content' => $data['content'],
        ];

        return Letter::create($emailInfo);
    }


    public function getLetterInfoByEmailAndUsernameAndId($email,$username,$id){
        return Letter::where('email',$email)->where('username',$username)->where('id',$id)->first();
    }
}
